﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem
{
    public partial class AddKaoHeEmpWindow : ChildWindow
    {
        public string OrgIDs = "";
        private JinShengService.JinShengServiceClient _Client = new JinShengService.JinShengServiceClient();
        public ObservableCollection<JinShengService.Emp_BasicInfoModel> SelectItems = new ObservableCollection<JinShengService.Emp_BasicInfoModel>();
        public AddKaoHeEmpWindow(string orgids)
        {
            InitializeComponent();
            OrgIDs = orgids;
            _Client.GetEmpBasicInfoAccordingToDutyCompleted += 
                new EventHandler<JinShengService.GetEmpBasicInfoAccordingToDutyCompletedEventArgs>(_Client_GetEmpBasicInfoAccordingToDutyCompleted);
            tbCondition.AddHandler(TextBox.MouseLeftButtonDownEvent, new MouseButtonEventHandler(TextBox_MouseLeftButtonDown), true);
        }

        private void ChildWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                btnAdminSearch_Click(null, null);
            }
        }

        private void TextBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbCondition.Text) && tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                tbCondition.SelectAll();
            }
        }

        private void tbCondition_KeyDown(object sender, KeyEventArgs e)
        {
            if (!string.IsNullOrEmpty(tbCondition.Text) && tbCondition.Text.Equals("请输入姓名或证件号码"))
            {
                tbCondition.FontStyle = FontStyles.Normal;
                //tbCondition.Text = e.Key.ToString();
                tbCondition.Foreground = new SolidColorBrush(Colors.Black);
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            SelectItems = null;
            var source = lb.ItemsSource as System.Collections.ObjectModel.ObservableCollection<CommunitySystem.JinShengService.Emp_BasicInfoModel>;
            SelectItems = new ObservableCollection<JinShengService.Emp_BasicInfoModel>(source.Where(c => c.IsSelected == true));
            if (SelectItems.Count > 0)
            {
                if (MessageBox.Show("确定添加任职提前考核人员吗？", "系统提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    this.DialogResult = true;
                }
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要添加的任职提前考核人员！");
            
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        private void btnAdminSearch_Click(object sender, RoutedEventArgs e)
        {
            string nameOrCard = "1=1";
            if (!string.IsNullOrEmpty(tbCondition.Text) && !tbCondition.Text.Equals("请输入姓名或证件号码"))  
            {
                nameOrCard = tbCondition.Text;
                _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(@" OrgID in ({0}) and KeChuJi = 0 and (Name like '%{1}%' or CardNo like '%{1}%') and DutyAttribute = '领导职务'
                and (IsKaoHe = 0 or iskaohe is null)", OrgIDs, nameOrCard));
                // _empClient.GetEmp_BasicInfoAsync(_OrgID + "", "0", "", "", 0, 20, "在职", string.Format("emp.Name like '%{0}%' or emp.CardNo like '%{0}%'", nameOrCard));
            }
            else
            {
                _Client.GetEmpBasicInfoAccordingToDutyAsync(string.Format(" OrgID in ({0}) and KeChuJi = 0 and DutyAttribute = '领导职务' and (IsKaoHe = 0 or iskaohe is null)", OrgIDs));
            }
        }

        void _Client_GetEmpBasicInfoAccordingToDutyCompleted(object sender, JinShengService.GetEmpBasicInfoAccordingToDutyCompletedEventArgs e)
        {
            lb.ItemsSource = null;
            lb.ItemsSource = e.Result;
        }
    }
}
